package utils
{
	import mx.formatters.DateFormatter;
	import mx.utils.ObjectUtil;

	public class SortUtil
	{
		public function SortUtil()
		{
		}
		
		
		/**
		 * 根据field来排序
		 **/
		public static function sortForField(field:String):Function
		{
			return function(obj1:Object, obj2:Object):int
			{
				if ( field == "dSN" )
				{
					return sortBySNHandler(obj1, obj2);
				}
				else if (field == "expire_date")
				{
					return sortByTimeHandler(obj1, obj2);
				}
				else
				{
					return ObjectUtil.stringCompare(obj1[field], obj2[field], true);
				}
			}
		}  
		
		private static function sortByTimeHandler(obj1:Object, obj2:Object):int
		{
			if (obj1.expire_date != null && obj2.expire_date != null) {
				
				var date1:Date = DateFormatter.parseDateString(obj1.expire_date);
				var date2:Date = DateFormatter.parseDateString(obj2.expire_date);
				if ( date1.time > date2.time )
				{
					return 1;
				}
			}
			return -1;
		}
		
		private static function sortBySNHandler(obj1:Object, obj2:Object):int
		{	
			if (obj1.sn != null && obj2.sn != null) {
				
				if ( int(obj1.sn) > int(obj2.sn) )
				{
					return 1;
				}
			}
			return -1;
		}
	}
}